﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="PopularByCategory.aspx.cs" Inherits="WebApplicationRating.PopularByCategory" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="imageid" DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display." 
        style="margin-right: 0px">
        <Columns>
            <asp:BoundField DataField="imageid" HeaderText="ImageId" InsertVisible="False" 
                ReadOnly="True" SortExpression="imageid" />
            <asp:BoundField DataField="name" HeaderText="Name" 
                SortExpression="name" />
            <asp:BoundField DataField="categoryid" HeaderText="CategoryId" 
                SortExpression="categoryid" />
            <asp:BoundField DataField="category_name" HeaderText="Category" 
                SortExpression="category_name" />
            <asp:BoundField DataField="AvgRating" HeaderText="AvgRating" ReadOnly="True" Visible="false"
                SortExpression="AvgRating" DataFormatString="{0:n2}" />   
         <asp:TemplateField HeaderText="Image">
         <ItemTemplate>
           <asp:Image ID="Image1" runat="server" Width='150'
           ImageUrl='<%# "Handler.ashx?imageid=" + Eval("imageid")%>'/>
         </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Stars">
<ItemTemplate>
<asp:Image ID="Stars" runat="server" ImageUrl='
<%# "~/Styles/Images/" + ThumbDisplayOne(Convert.ToDecimal((Eval("AvgRating"))))
+ ThumbDisplayTwo(Convert.ToDecimal((Eval("AvgRating"))))
+ ThumbDisplayThree(Convert.ToDecimal((Eval("AvgRating"))))
+ ThumbDisplayFour(Convert.ToDecimal((Eval("AvgRating"))))
+ ThumbDisplayFive(Convert.ToDecimal((Eval("AvgRating"))))
%>'/>
</ItemTemplate>   
</asp:TemplateField> 
<asp:TemplateField HeaderText="+">
 <ItemTemplate>
   <asp:Label ID="StarDecimal" Text='<%# Convert.ToString(Eval("AvgRating")).Substring(1,3) %>' runat="server" Font-Bold="true" foreColor="Orange" />&nbsp;&nbsp;
</ItemTemplate>
</asp:TemplateField>
</Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:imageConnectionString1 %>" 
        DeleteCommand="DELETE FROM [UserImageRating] WHERE [user_image_rating_id] = @user_image_rating_id" 
        InsertCommand="INSERT INTO [UserImageRating] ([userid], [imageid], [rating], [rating_date]) VALUES (@userid, @imageid, @rating, @rating_date)" 
        ProviderName="<%$ ConnectionStrings:imageConnectionString1.ProviderName %>" 
        SelectCommand="SELECT imageid, name, categoryid, category_name, AvgRating, image_file FROM (SELECT TOP (100) PERCENT Image.imageid, Image.name, derivedtbl_2.categoryid, derivedtbl_2.category_name, derivedtbl_2.Expr1 AS AvgRating, Image.image_file FROM (SELECT categoryid, imageid, category_name, AVG(rating) AS Expr1 FROM (SELECT ImageCategory.categoryid, Category.category_name, ImageCategory.imageid, UserImageRating.rating FROM ImageCategory INNER JOIN UserImageRating ON ImageCategory.imageid = UserImageRating.imageid INNER JOIN Category ON Category.categoryid = ImageCategory.categoryid) AS derivedtbl_1 GROUP BY categoryid, category_name, imageid) AS derivedtbl_2 INNER JOIN Image ON derivedtbl_2.imageid = Image.imageid) AS derivedtbl_3 ORDER BY AvgRating DESC, categoryid" 
        
        UpdateCommand="UPDATE [UserImageRating] SET [userid] = @userid, [imageid] = @imageid, [rating] = @rating, [rating_date] = @rating_date WHERE [user_image_rating_id] = @user_image_rating_id">
        <DeleteParameters>
            <asp:Parameter Name="user_image_rating_id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="userid" Type="Int32" />
            <asp:Parameter Name="imageid" Type="Int32" />
            <asp:Parameter Name="rating" Type="Int32" />
            <asp:Parameter Name="rating_date" Type="DateTime" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="userid" Type="Int32" />
            <asp:Parameter Name="imageid" Type="Int32" />
            <asp:Parameter Name="rating" Type="Int32" />
            <asp:Parameter Name="rating_date" Type="DateTime" />
            <asp:Parameter Name="user_image_rating_id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    </asp:Content>
